<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "taozhuang".
 *
 * @property integer $sid
 * @property string $name
 * @property string $tips
 * @property string $price_base
 * @property string $price_custom
 * @property string $related_list
 * @property integer $app_return
 * @property integer $pos_type
 * @property integer $img_small_id
 * @property integer $img_thumb_id
 * @property integer $img_url_id
 * @property integer $space_id
 * @property integer $top_time
 */
class Taozhuang extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'taozhuang';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['price_base', 'price_custom'], 'number'],
            [['app_return', 'pos_type', 'img_small_id', 'img_thumb_id', 'img_url_id', 'space_id', 'top_time'], 'integer'],
            [['name'], 'string', 'max' => 64],
            [['tips'], 'string', 'max' => 512],
        	[['lifestyle'],'string', 'max' => 4096],
            [['related_list'], 'string', 'max' => 128]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'sid' => Yii::t('app', 'Taozhuang Sid'),
            'name' => Yii::t('app', 'Taozhuang Name'),
            'tips' => Yii::t('app', 'Taozhuang Tips'),
            'price_base' => Yii::t('app', 'Taozhuang Price Base'),
            'price_custom' => Yii::t('app', 'Taozhuang Price Custom'),
            'related_list' => Yii::t('app', 'Taozhuang Related List'),
            'app_return' => Yii::t('app', 'Taozhuang App Return'),
            'pos_type' => Yii::t('app', 'Taozhuang Pos Type'),
            'img_small_id' => Yii::t('app', 'Taozhuang Img Small ID'),
            'img_thumb_id' => Yii::t('app', 'Taozhuang Img Thumb ID'),
            'img_url_id' => Yii::t('app', 'Taozhuang Img Url ID'),
            'space_id' => Yii::t('app', 'Taozhuang Space ID'),
            'top_time' => Yii::t('app', 'Taozhuang Top Time'),
        ];
    }
    public function getTzSmallImage(){
    	return $this->hasOne(Image::className(),['id'=>'img_small_id']);//显示小图
    }
    public function getTzThumbImage(){
        return $this->hasOne(Image::className(), ['id'=>'img_thumb_id']);
    }
    public function getTzUrlImage()
    {
        return $this->hasOne(Image::className(), ['id'=>'img_url_id']);
    }
	public function getSpaceName(){
    	return $this->hasOne(GrpSpace::className(),['id'=>'space_id']);//显示空间名
    }
    public static function show_tz_small_img($sid){
    	if($model = self::findOne(['sid'=>$sid])){
    		$img_small_id =$model ->img_small_id;
    	}
    	if(!empty($img_small_id))
    	return Image::show_image($img_small_id);
    }
    /*通过套装id获取space_id*/
    public static function get_space_id($sid){
    	return self::findOne(['sid'=>$sid])->space_id;
    	   
    }
    public static function generateQRcode($sid){
        $url = 'https://www.jajahome.com/gjj/frontend/web/tz/tz?sid='.$sid;
    	$QRcode = Qr::generate_admin($url,'taozhuang');  	 
    	Yii::$app->db->createCommand()->update('taozhuang', ['QRcode'=>$QRcode],"sid={$sid}")->execute();    	 
    }
}
